class PostsController < ApplicationController
    
  def index
     @posts = Post.all
  end

  def new
    @post = Post.new
  end

    def create
        @post = Post.new(:content => params[:post][:content], :topic_id => params[:post][:topic_id], :nombre_autor => params[:post][:nombre_autor])
        if @post.save
            @topic = Topic.find(@post.topic_id)
            @topic.update_attributes(:last_post_at => Time.now)
            flash[:notice] = "Successfully created post."
            redirect_to "/topics/#{@post.topic_id}"
            else
            render :action => 'new'
        end
    end

  def edit
    @post = Post.find(params[:id])
  end

    def update
        @post = Post.find(params[:id])
        if @post.update_attributes(params[:post])
            @topic = Topic.find(@post.topic_id)
            @topic.update_attributes(:last_poster_id => current_user.id, :last_post_at => Time.now)
            flash[:notice] = "Successfully updated post."
            redirect_to @post
        else
            render :action => 'edit'
        end
    end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to forums_url, :notice => "Successfully destroyed post."
  end
end
